package cn.yulr.v4l2multicamera;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainActivity extends Activity {

    private V4L2Camera v4L2Camera1;
    private V4L2Camera v4L2Camera2;
    private Camera frontCamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        v4L2Camera2 = new V4L2Camera("/dev/video1", V4L2Camera.ImageFormat.V4L2_PIX_FMT_NV12, 1280, 960);

        ((SurfaceView) findViewById(R.id.surface02)).getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                v4L2Camera2.init();
                v4L2Camera2.setDisplaySurfaceView((SurfaceView) findViewById(R.id.surface02), V4L2Camera.DisplayType.FIXED, 1);
                v4L2Camera2.startPreview();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });

//        frontCamera = Camera.open(1);
//
//        ((SurfaceView) findViewById(R.id.surface02)).getHolder().addCallback(new SurfaceHolder.Callback() {
//            @Override
//            public void surfaceCreated(SurfaceHolder surfaceHolder) {
//
//                if (frontCamera != null) {
//                    Camera.Parameters parameters = frontCamera.getParameters();
//                    parameters.setPreviewSize(1280, 960);
////                            * @see #FOCUS_MODE_AUTO
////                            * @see #FOCUS_MODE_INFINITY
////                            * @see #FOCUS_MODE_MACRO
////                            * @see #FOCUS_MODE_FIXED
////                            * @see #FOCUS_MODE_EDOF
////                            * @see #FOCUS_MODE_CONTINUOUS_VIDEO
//                    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
//                    frontCamera.setParameters(parameters);
//                    try {
//                        frontCamera.setPreviewDisplay(((SurfaceView) findViewById(R.id.surface02)).getHolder());
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                    frontCamera.startPreview();
//
//                }
//
//            }
//
//            @Override
//            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
//
//            }
//
//            @Override
//            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
//
//            }
//        });

//        v4L2Camera1 = new V4L2Camera("/dev/video1", V4L2Camera.ImageFormat.V4L2_PIX_FMT_NV12, 2048, 1536);
        v4L2Camera1 = new V4L2Camera("/dev/video0", V4L2Camera.ImageFormat.V4L2_PIX_FMT_NV12, 1280, 960);
//        v4L2Camera1 = new V4L2Camera("/dev/video0", V4L2Camera.ImageFormat.V4L2_PIX_FMT_NV12, 640, 480);

        ((SurfaceView) findViewById(R.id.surface01)).getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                v4L2Camera1.init();
                v4L2Camera1.setDisplaySurfaceView((SurfaceView) findViewById(R.id.surface01), V4L2Camera.DisplayType.FIXED, 1);
                v4L2Camera1.startPreview();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });

    }

    @Override
    protected void onStop() {
        super.onStop();
        if (v4L2Camera1 != null) {
            v4L2Camera1.stopPreview();
            v4L2Camera1.release();
        }
        if (v4L2Camera2 != null) {
            v4L2Camera2.stopPreview();
            v4L2Camera2.release();
        }

        if (this.frontCamera != null) {
            this.frontCamera.stopPreview();
            this.frontCamera.release();
            this.frontCamera = null;
        }

    }
}
